package com.harveyh.cemax.manager;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.harveyh.cemax.bean.ShiTiBean;
import com.harveyh.cemax.common.GloableData;

public class OtherDataDBManager {
	private Context ctx;
	public final String KEY_id = "id", KEY_jibie = "jibie", 
			KEY_type = "type", KEY_timu = "timu", KEY_A = "A", KEY_B = "B",
			KEY_C = "C", KEY_D = "D", KEY_daan = "daan", KEY_daan_desc = "daan_desc",
			KEY_tupian = "tupian", KEY_dui = "dui", KEY_cuo = "cuo", KEY_cuoti = "cuoti";
	private final String[] columns = new String[] { KEY_id,KEY_jibie, 
			KEY_type, KEY_timu,KEY_A, KEY_B,
			KEY_C , KEY_D , KEY_daan , KEY_daan_desc,
			KEY_tupian, KEY_dui, KEY_cuo, KEY_cuoti};


	private SQLiteDatabase db;

	public OtherDataDBManager(Context ctx) {
		this.ctx = ctx;
		File file = new File(GloableData.DB_DIR + GloableData.DB_NAME);
		if (file.exists()) {
			db = SQLiteDatabase.openDatabase(GloableData.DB_DIR + GloableData.DB_NAME, null,SQLiteDatabase.OPEN_READWRITE);
		}
	}

	public void close() {
		if (db != null) {
			db.close();
		}
	}
	/** 将sd卡上的数据库导入到本地数据库
	 * @return 是否成功导入
	 * */
	public boolean importDBFromDBFile() {
		if (db!=null) {
			DBManager localDB = DBManager.open(ctx);
			localDB.beginTransaction();
			try {// 这里可以尝试用每张表单独开一个线程的方式去优化
				long oneStart = System.currentTimeMillis();
				importDBFromTable(GloableData.chujiTableName,localDB);
				UtilsManager.println("第一张表耗时："+(System.currentTimeMillis()-oneStart));
				long twoStart = System.currentTimeMillis();
				importDBFromTable(GloableData.gaojiTableName,localDB);
				UtilsManager.println("第2张表耗时："+(System.currentTimeMillis()-twoStart));
				long t3 = System.currentTimeMillis();
				importDBFromTable(GloableData.gaojijishiTableName,localDB);
				UtilsManager.println("第3张表耗时："+(System.currentTimeMillis()-t3));
				long t4 = System.currentTimeMillis();
				importDBFromTable(GloableData.jigongTableName,localDB);
				UtilsManager.println("第4张表耗时："+(System.currentTimeMillis()-t4));
				long t5 = System.currentTimeMillis();
				importDBFromTable(GloableData.zhongjiTableName,localDB);
				UtilsManager.println("第5张表耗时："+(System.currentTimeMillis()-t5));
				localDB.setTransactionSuccessful();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			} finally {
				localDB.endTransaction();
			}
		}
		return false;
	}
	
	private void importDBFromTable(String tableName,DBManager localDB) {
//		ArrayList<ShiTiBean> list = null;
		Cursor cur = db.query(tableName, columns, null, null, null, null, null);
		if (cur!=null) {
			if (cur.moveToFirst()) {
//				list = new ArrayList<ShiTiBean>(cur.getCount());
				ShiTiBean bean;
				do {
					bean = new ShiTiBean();
					bean.id = cur.getInt(0);
					bean.jibie = cur.getString(1);
					bean.timuType = cur.getString(2);
					bean.timu = cur.getString(3);
					bean.optionA = cur.getString(4);
					bean.optionB = cur.getString(5);
					bean.optionC = cur.getString(6);
					bean.optionD = cur.getString(7);
					bean.answerOption = cur.getString(8);
					bean.answerDesc = cur.getString(9);
					bean.picUrl = cur.getString(10);
					bean.duiCount = cur.getInt(11);
					bean.cuoCount = cur.getInt(12);
					bean.isCuoti = cur.getInt(13);
//					list.add(bean);
//					UtilsManager.println("insert to db:"+bean.id+","+bean.timu);
					localDB.insertOneTimu2DB(tableName, bean);// 循环插入本地数据库
				} while (cur.moveToNext());
			}
			cur.close();
		}
	}
	
}
